import Page from './page'
import { yyapi, kRoots, kWinHints } from './api'
import { ChannelEvent } from './events'
import { showLogin } from './utils'

const Channels = Page.extend(function () {
  var self = this
  var _channel = null
  var _channelTree = null
  var _sid = 0
  var _ssid = 0

  // var _workerStr = `
  //   addEventListener("message", function (e) {
  //     console.info('111111======================', e.data)
  //     postMessage('some message');
  //   }, false);
  // `
  // var _worker = null

  // var startWorker = function () {
  //   var blob = new Blob([_workerStr])
  //   var url = window.URL.createObjectURL(blob)
  //   _worker = new Worker(url)

  //   _worker.onmessage = function (e) {
  //     console.info('00000000======================', e.data)
  //   }
  //   _worker.onerror = function (e) {
  //     console.info('00000000======================', e.data)
  //   }
  // }

  // var sendWorker = function (data) {
  //   _worker && _worker.postMessage(data)
  // }
  var doEvent = function (key, data) {
    var event = new ChannelEvent(key)
    event.eventData = data
    self.dispatch(key, event)
  }

  var initPage = function () {
    self.removeStyle(kWinHints.kTopMostHint)
    self.addStyle(kWinHints.kResizableHint)
    self.hide()
    self.resize(800, 600)
    // self.setMinSize(800, 600)
    self.align(4)
    self.setNCBorderWidth(2)
    self.setCaptionArea([[0, 0, 800 - 120, 50]])
    if (typeof window !== 'undefined') {
      window.oncontextmenu = function (event) {
        event.returnValue = false
      }
      window.ondragstart = function (event) {
        return false
      }
      window.addEventListener('resize', function () {
        self.setCaptionArea([[0, 0, window.innerWidth - 120, 50]])
      })
    }
  }

  var onChannelLeaved = function (sid, subSid) {
    self.closeWindow()
  }

  var onChatPublic = function (msg) {
    doEvent(ChannelEvent.CHAT_PUBLIC, msg)
  }

  this.init = function () {
    this.super.init()
    if (!self.isLogined()) {
      showLogin()
      return
    }
    initPage()
    _channel = yyapi.impl().roots().get(kRoots.Channel.Key)
    _channel && _channel.onSignal('onChannelLeaved', onChannelLeaved)
    _channel && _channel.onSignal('onChatPublic', onChatPublic)
    _channelTree = _channel && _channel.excute('channelTree', [])
    // startWorker()
  }

  this.start = function (vue) {
    this.super.start(vue)
    self.show()

    // sendWorker('dafgagasdgasdgasdgasd')
  }

  this.leaveChannel = function () {
    _channel && _channel.excute('leaveChannel', [])
  }

  this.sid = function () {
    return _sid || (_sid = _channel && _channel.excute('getSid', []))
  }

  this.ssid = function () {
    return _ssid || (_ssid = _channel && _channel.excute('getSubSid', []))
  }

  this.closeWindow = function () {
    self.leaveChannel()
    this.super.closeWindow()
  }

  this.treeInfo = function (rootSid) {
    rootSid = rootSid || 0
    return _channelTree && _channelTree.getTreeInfo(rootSid)
  }

  this.sendMsgPublic = function (msg, fontname, size, color) {
    _channel && _channel.excute('sendMsgPublic', [msg, fontname, size, color])
  }
})

var _main = null

Channels.impl = function () {
  return _main || (_main = new Channels())
}

export default Channels
